// BeamQualityTypes.h
#pragma once
#include <bitset>
#include <QImage>
#include <QString>
#include <QTime>


// 当前平面参数
struct CurrentPlaneParams {
    int Number = 0;
    double CentroidX = 0.0;
    double CentroidY = 0.0;
    double PositionZ = 0.0;
    double WidthGeneralized = 0.0;
    double WidthMajor = 0.0;
    double WidthMinor = 0.0;
    double AzimuthAngle = 0.0;
    double Ellipticity = 0.0;
    double Eccentricity = 0.0;
    QImage my_image;   //3D图像
    char vis = 1;
};


struct BeamQualityDataBatch {
    std::vector<double>  centroidX{0.0};  // 质心X
    std::vector<double>  centroidY{0.0};  // 质心Y
    std::vector<double> positionZ{0.0};  // Z位置数据
    std::vector<double>  widthGeneralized{0.0};
    std::vector<double>  diameterMajorValue{0.0};  // 长轴直径
    std::vector<double>  diameterMinorValue{0.0};  // 短轴直径
    std::vector<double>  azimuthAngleValue{0.0};
    std::vector<double> ellipticityValue{0.0};
    std::vector<double>  eccentricityValue{0.0};
    std::vector<QImage>  my_image;
    std::vector<char> vis{0};

    // 添加项目数据
    void addItem(int index, float z, const CurrentPlaneParams& params) {
        positionZ.push_back(z);
        widthGeneralized.push_back(params.WidthGeneralized);
        centroidX.push_back(params.CentroidX);
        centroidY.push_back(params.CentroidY);
        diameterMajorValue.push_back(params.WidthMajor);
        diameterMinorValue.push_back(params.WidthMinor);
        ellipticityValue.push_back(params.Ellipticity);
        eccentricityValue.push_back(params.Eccentricity);
        azimuthAngleValue.push_back(params.AzimuthAngle);
        my_image.push_back(params.my_image);
        vis.push_back(1);
    }

    // 删除指定索引的项目
    void removeItem(int index) {
        if (index<0||index>positionZ.size()) {
            return;
        }
            positionZ.erase(positionZ.begin() + index);
            centroidX.erase(centroidX.begin() + index);
            centroidY.erase(centroidY.begin() + index);
            widthGeneralized.erase(widthGeneralized.begin() + index);
            diameterMajorValue.erase(diameterMajorValue.begin() + index);
            diameterMinorValue.erase(diameterMinorValue.begin() + index);
            ellipticityValue.erase(ellipticityValue.begin() + index);
            eccentricityValue.erase(eccentricityValue.begin() + index);
            azimuthAngleValue.erase(azimuthAngleValue.begin() + index);
            my_image.erase(my_image.begin() + index);
            vis.erase(vis.begin() + index);

    }
    // 清空所有数据
    void clear() {
        positionZ.clear();
        diameterMajorValue.clear();
        diameterMinorValue.clear();
        ellipticityValue.clear();
        eccentricityValue.clear();
        azimuthAngleValue.clear();
        my_image.clear();
        centroidX.clear();
        centroidY.clear();
        widthGeneralized.clear();
        vis.clear();
    }
    void swapItem(int lhs,int rhs) {
        std::swap(positionZ[lhs],positionZ[rhs]);
        std::swap(diameterMajorValue[lhs],diameterMajorValue[rhs]);
        std::swap(diameterMinorValue[lhs],diameterMinorValue[rhs]);
        std::swap(ellipticityValue[lhs],ellipticityValue[rhs]);
        std::swap(eccentricityValue[lhs],eccentricityValue[rhs]);
        std::swap(azimuthAngleValue[lhs],azimuthAngleValue[rhs]);
        std::swap(my_image[lhs],my_image[rhs]);
        std::swap(centroidX[lhs],centroidX[rhs]);
        std::swap(centroidY[lhs],centroidY[rhs]);
        std::swap(widthGeneralized[lhs],widthGeneralized[rhs]);
        std::swap(vis[lhs],vis[rhs]);
    }



};

struct BeamQualityResult {
    // Current Plane
    int number = 0;
    double CentroidX = 0;
    double CentroidY = 0;
    double PositionZ = 0;
    double WidthGeneralized = 0;
    double WidthMajor = 0;
    double WidthMinor = 0;
    double AzimuthAngle = 0;
    double Ellipticity = 0;
    double Eccentricity = 0;

    // General
    int NumberofPositions = 0;
    QString GeometricBeamType;
    QString Results;

    // Torsion
    int N_elliptical = 0;
    double Torsion = 0.0;

    // U Caustic
    QString uISO11146_1;
    QString uCausticPosition;
    QString uCausticPosition_Uncertainty;
    QString uBeamWaistDiameter;
    QString uBeamWaistDiameter_Uncertainty;
    QString uRayleighLength;
    QString uRayleighLength_Uncertainty;
    QString uDivergence;
    QString uDivergence_Uncertainty;
    QString uBeamParameterProduct;
    QString uBeamParameterProduct_Uncertainty;
    QString uBeamPropagationRatio;
    QString uBeamPropagationRatio_Uncertainty;
    QString uBeamPropagationFactor;
    QString uBeamPropagationFactor_Uncertainty;

    // V Caustic
    QString vISO11146_1;
    QString vCausticPosition;
    QString vCausticPosition_Uncertainty;
    QString vBeamWaistDiameter;
    QString vBeamWaistDiameter_Uncertainty;
    QString vRayleighLength;
    QString vRayleighLength_Uncertainty;
    QString vDivergence;
    QString vDivergence_Uncertainty;
    QString vBeamParameterProduct;
    QString vBeamParameterProduct_Uncertainty;
    QString vBeamPropagationRatio;
    QString vBeamPropagationRatio_Uncertainty;
    QString vBeamPropagationFactor;
    QString vBeamPropagationFactor_Uncertainty;

    // UV Caustic
    QString AstigmatismRel;
    QString AstigmatismRel_Uncertainty;
    QString Asymmetry;
    QString Asymmetry_Uncertainty;

    //Casutic Fit
    QString Caustic_Position;
    QString Caustic_Position_Uncertainty;
    QString Beam_Waist_Diameter;
    QString Beam_Waist_Diameter_Uncertainty;
    QString Rayleigh_Length;
    QString Rayleigh_Length_Uncertainty;
    QString Divergence;
    QString Divergence_Uncertainty;

    //Caustic 3D Fit
    QString ISO11146_2 ;
    QString Reyleigh_Length_3D_left;
    QString Reyleigh_Length_3D_left_Uncertainty;
    QString Reyleigh_Length_3D_right;
    QString Reyleigh_Length_3D_right_Uncertainty;
    QString Twist_3D;
    QString Twist_3D_Uncertainty;
    QString Astigmatism_rel_3D;
    QString Astigmatism_rel_3D_Uncertainty;
    QString Astigmatism_Angle_3D;
    QString Astigmatism_Angle_3D_Uncertainty;
    QString Divergence_3D;
    QString Divergence_3D_Uncertainty;
    QString Beam_Parameter_Product_3D;
    QString Beam_Parameter_Product_3D_Uncertainty;
    QString Beam_Propagation_Ratio_3D;
    QString Beam_Propagation_Ratio_3D_Uncertainty;
    QString Beam_Propagation_Factor_3D;
    QString Beam_Propagation_Factor_3D_Uncertainty;
    QString Intrinsic_Astigmatism_3D;
    QString Intrinsic_Astigmatism_3D_Uncertainty;



};



// 通用参数
struct GeneralParams {
    int NumberofPositions = 0;
    QString GeometricBeamType;
    QString Results;
};

// 扭曲参数
struct TorsionParams {
    int N_elliptical = 0;
    double Torsion = 0.0;
};


struct CausticParamsBase {
    QString ISO11146_1;
    QString CausticPosition;
    QString CausticPosition_Uncertainty;
    QString BeamWaistDiameter;
    QString BeamWaistDiameter_Uncertainty;
    QString RayleighLength;
    QString RayleighLength_Uncertainty;
    QString Divergence;
    QString Divergence_Uncertainty;
    QString BeamParameterProduct;
    QString BeamParameterProduct_Uncertainty;
    QString BeamPropagationRatio;
    QString BeamPropagationRatio_Uncertainty;
    QString BeamPropagationFactor;
    QString BeamPropagationFactor_Uncertainty;
};

// U参数（特化）
struct UCausticParams : public CausticParamsBase {

};

// V参数（特化）
struct VCausticParams : public CausticParamsBase {

};

// UV参数
struct UVCausticParams {
    QString AstigmatismRel;
    QString AstigmatismRel_Uncertainty;
    QString Asymmetry;
    QString Asymmetry_Uncertainty;
};


// Caustic Fit
struct CausticFitParams {
    QString CausticPosition;
    QString CausticPosition_Uncertainty;
    QString BeamWaistDiameter;
    QString BeamWaistDiameter_Uncertainty;
    QString RayleighLength;
    QString RayleighLength_Uncertainty;
    QString Divergence;
    QString Divergence_Uncertainty;
};

// Caustic 3D Fit
struct Caustic3DFitParams {
    QString ISO11146_2;
    QString ReyleighLengthLeft;
    QString ReyleighLengthLeft_Uncertainty;
    QString ReyleighLengthRight;
    QString ReyleighLengthRight_Uncertainty;
    QString Twist;
    QString Twist_Uncertainty;
    QString AstigmatismRel;
    QString AstigmatismRel_Uncertainty;
    QString AstigmatismAngle;
    QString AstigmatismAngle_Uncertainty;
    QString Divergence3D;
    QString Divergence3D_Uncertainty;
    QString BeamParameterProduct;
    QString BeamParameterProduct_Uncertainty;
    QString BeamPropagationRatio;
    QString BeamPropagationRatio_Uncertainty;
    QString BeamPropagationFactor;
    QString BeamPropagationFactor_Uncertainty;
    QString IntrinsicAstigmatism;
    QString IntrinsicAstigmatism_Uncertainty;
};


// 完整的光束质量结果参数
struct BeamQualityResultParams {
    CurrentPlaneParams currentPlane;
    GeneralParams general;
    TorsionParams torsion;
    UCausticParams uCaustic;
    VCausticParams vCaustic;
    UVCausticParams uvCaustic;
    CausticFitParams causticFit;
    Caustic3DFitParams caustic3DFit;
};


// 扫描配置
struct ScanConfiguration {
    double startPosition;        // 扫描起始位置 (mm)
    double endPosition;          // 扫描结束位置 (mm)
    double scanSpeed;            // 扫描速度 (mm/s)
    double frameRateMultiplier;  // 帧率倍数（默认2.0）
    QString dataFolderPath;      // 数据保存路径

    ScanConfiguration()
        : startPosition(0.0), endPosition(10.0), scanSpeed(5.0),
          frameRateMultiplier(2.0) {}
};

// 扫描数据点
struct ScanDataPoint {
    double position;        // Z轴位置 (mm)
    double beamWidthX;      // X方向光束宽度 (mm)
    double beamWidthY;      // Y方向光束宽度 (mm)
    double intensity;       // 光强
    QDateTime timestamp;    // 时间戳

    ScanDataPoint()
        : position(0.0), beamWidthX(0.0), beamWidthY(0.0),
          intensity(0.0), timestamp(QDateTime::currentDateTime()) {}

    ScanDataPoint(double pos, double widthX, double widthY, double intens = 1.0)
        : position(pos), beamWidthX(widthX), beamWidthY(widthY),
          intensity(intens), timestamp(QDateTime::currentDateTime()) {}
};

// 光束拟合参数
struct BeamFittingParams {
    double z0;              // 光束腰位置 (mm)
    double w0;              // 光束腰半径 (mm)
    double zr;              // 瑞利长度 (mm)
    double m2;              // 光束质量因子
    double bpp;             // 光束参数乘积 (mm·mrad)
    double wavelength;      // 波长 (m)

    // 拟合质量评估
    double rSquared;        // 拟合优度
    double rmse;            // 均方根误差
    bool isValid;           // 拟合结果是否有效

    BeamFittingParams()
        : z0(0.0), w0(0.0), zr(0.0), m2(1.0), bpp(0.0),
          wavelength(1550e-9), rSquared(0.0), rmse(0.0), isValid(false) {}
};

// 测量结果集合
struct BeamQualityMeasurementResults {
    BeamFittingParams paramsX;
    BeamFittingParams paramsY;
    QVector<ScanDataPoint> scanData;
    QVector<double> measurementPositions;
    QString dataFilePath;
    QDateTime measurementTime;

    BeamQualityMeasurementResults()
        : measurementTime(QDateTime::currentDateTime()) {}
};


